/*
 * @Author: 杨三炮
 * @Date: 2021-11-20 13:32:48
 * @LastEditors: 杨三炮
 * @LastEditTime: 2021-11-23 18:45:17
 * @FilePath: \node\node\dao\student_dao.js
 */

const dayjs = require('dayjs')
module.exports = class student_dao extends require('../model/student_mod') {
    /**
     * @description: 我的通知分页获取数据与数量
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async getNotice (req, res) {
        console.log(req.user);
        let class_id = req.user.class_id
        try {
            let data = await this.getNoticeMod(class_id)
            let isdu = await this.getNoticeReadMod(class_id)
            res.json({
                msg: '获取成功',
                code: 200,
                data,
                total: data.length,
                isdu,
            })
        } catch (error) {
            res.json(err)
        }
    }


    /**
     * @description: 获取的我通知已读列表(供已读未读状态渲染)
     * @param {*}
     * @return {*}
     */
    static async getNoticeRead (req, res) {
        let u_id = req.user.id
        const data = await this.getNoticeReadMod(u_id)
        res.json(data)
    }
    /**
     * @description: 已读转未读
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async goweidu (req, res) {
        let u_id = req.user.id
        let n_id = req.query.n_id
        try {
            const result = await this.goweiduMod(u_id, n_id)
            res.json(result)
        } catch (error) {
            res.json(error)
        }
    }
    /**
     * @description: 未读转已读
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async goyidu (req, res) {
        let u_id = req.user.id
        let n_id = req.query.n_id
        try {
            const result = await this.goyiduMod(u_id, n_id)
            res.json(result)
        } catch (error) {
            res.json(error)
        }
    }

    /**
     * @description: 健康填报表提交
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async sethealth (req, res) {
        req.body.temperature = parseFloat(req.body.temperature)
        let healthInfo = {
            u_id: req.user.id,
            temperature: req.body.temperature,
            hot: req.body.hot,
            gohubei: req.body.gohubei,
            hubeiren: req.body.hubeiren,
            fever: req.body.fever,
            leaveout: req.body.leaveout,
            hesuan: req.body.hesuan,
            mask: req.body.mask,
            masknum: req.body.masknum,
            kills: req.body.kills,
        }
        try {
            const result = await this.sethealthMod(healthInfo)
            res.json({
                code: 200,
                msg: '提交成功',
                result,
            })
        } catch (error) {
            res.status(404).json('服务器发生异常')
        }

    }
    /**
     * @description: 获取当天所有报表
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async gethealthNowDayPage (req, res) {
        let pageNum = req.query.pageNum
        let currPage = req.query.currPage
        let date = new Date()
        let month = ''
        if ((date.getMonth() + 1) < 10) {
            month = `0` + String((date.getMonth() + 1))
        } else {
            month = (date.getMonth() + 1) + ''
        }
        let newDate = '' + date.getFullYear() + month + date.getDate()
        let lastDate = '' + date.getFullYear() + month + (date.getDate() + 1)

        const data = await this.gethealthNowDayPageMod(newDate, lastDate, pageNum, currPage)
        const total = await this.gethealthNowDayPageToalMod(newDate, lastDate)
        res.json({
            msg: '获取成功',
            code: 200,
            data,
            total
        })
    }
    /**
     * @description: 获取当天某用户的报表
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async getHealthNowDayByid (req, res) {
        let date = new Date()
        let month = ''
        if ((date.getMonth() + 1) < 10) {
            month = `0` + String((date.getMonth() + 1))
        } else {
            month = (date.getMonth() + 1) + ''
        }
        let newDate = '' + date.getFullYear() + month + date.getDate()
        let lastDate = '' + date.getFullYear() + month + (date.getDate() + 1)
        try {
            const data = await this.getHealthNowDayByidMod(req.user.id, newDate, lastDate)
            if (!data.length) {
                return res.json({
                    msg: '今天未填写',
                    code: 0,
                })
            }
            res.json({
                msg: '获取成功',
                code: 200,
                data,
            })
        } catch (error) {
            res.status(404).json('网络出现了问题')
        }

    }
    /**
     * @description: .获取当天所有填报表
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async gethealthNowDay (req, res) {
        let date = new Date()
        let month = ''
        if ((date.getMonth() + 1) < 10) {
            month = `0` + String((date.getMonth() + 1))
        } else {
            month = (date.getMonth() + 1) + ''
        }
        let newDate = '' + date.getFullYear() + month + date.getDate()
        let lastDate = '' + date.getFullYear() + month + (date.getDate() + 1)
        const data = await this.gethealthNowDayMod(newDate, lastDate)
        res.json({
            msg: '获取成功',
            code: 200,
            data,
        })
    }
    /**
     * @description: 获取当月所有报表
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async gethealthNowMonth (req, res) {
        let date = new Date()
        let month = ''
        if ((date.getMonth() + 1) < 10) {
            month = `0` + String((date.getMonth() + 1))
        } else {
            month = (date.getMonth() + 1) + ''
        }
        let firstMonthDay = '' + date.getFullYear() + month + '01'
        let lastMonthDay = ''
        switch (month) {
            case '1':
            case '3':
            case '5':
            case '7':
            case '8':
            case '10':
            case '12':
                lastMonthDay = '' + date.getFullYear() + month + '31'
                break;
            case '2':
                lastMonthDay = '' + date.getFullYear() + month + '28'
                break;
            default:
                lastMonthDay = '' + date.getFullYear() + month + '30'
                break;
        }

        const data = await this.gethealthNowMonthMod(firstMonthDay, lastMonthDay)
        res.json({
            msg: '获取成功',
            code: 200,
            data,
        })
    }
    /**
     * @description: 获取所有报表
     * @param {*} req
     * @param {*} res
     * @return {*}
     */
    static async getAllHealth (req, res) {
        const data = await this.getAllHealthMod()
        res.json({
            msg: '获取成功',
            code: 200,
            data,
        })
    }


    /**
     * @description: 
     * @param {*} reason    请假理由
     * @param {*} leavetype 请假类型
     * @param {*} starttime 开始时间
     * @param {*} endtime   结束时间
     * @return {*}
     */
    static async setLeave (req, res) {
        let starttime = dayjs(req.body.leavtTime[0]).format('YYYY-MM-DD HH:mm:ss')
        let endtime = dayjs(req.body.leavtTime[1]).format('YYYY-MM-DD HH:mm:ss')
        /*   res.json(req.body) */
        const data = {
            u_id: req.user.id,
            reason: req.body.leaveContent,
            leavetype: req.body.leaveType,
            starttime,
            endtime,
        }

        try {
            const result = await this.setLeaveMod(data)
            res.json({
                msg: '发送成功',
                code: 200,
                result
            })
        } catch (error) {
            res.status(404).json('服务器异常')
        }
    }
}